﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Model;
using MM.Common;

namespace MobileManager
{
    public partial class YYfeeStatistics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    BindSPbusi(ref ddlSP);
                    ddlSP.Items.Insert(0, new ListItem("全部", "-1"));

                    BindDdlChannel(ref ddlChannel);
                    ddlChannel.Items.Insert(0, new ListItem("全部", "-1"));

                    BindDdlODEP(ref ddlOP);
                    ddlOP.Items.Insert(0, new ListItem("全部", "-1"));

                    GVDataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void BindSPbusi(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT SNO,SPName FROM T_SPbusi WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "SNO";
            ddl.DataTextField = "SPName";
            ddl.DataBind();
        }
        private void BindDdlChannel(ref DropDownList ddl)
        {
            string strSQL = string.Empty;
            if (!ddlSP.SelectedValue.Equals("-1"))
                strSQL = "SELECT ChannelNO,ChannelNO+'-'+ ChannelNumber+'-'+ChannelOrder as Name FROM T_Channel WITH(NOLOCK) order by ChannelNO ";
            else strSQL = string.Format("SELECT ChannelNO,ChannelNO+'-'+ ChannelNumber+'-'+ChannelOrder as Name FROM T_Channel WITH(NOLOCK) WHERE SNO='{0}' order by ChannelNO ", ddlSP.SelectedValue);

            ddl.Items.Clear();
            ddl.DataSource = DbHelperSql.Query(strSQL).Tables[0];
            ddl.DataValueField = "ChannelNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void BindDdlODEP(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ONO,ONO+'-'+ OName as Name FROM T_ODEP WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "ONO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }

        protected void ddlSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDdlChannel(ref ddlChannel);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {

        }
        private void GVDataBind()
        {
            string strSQL = "SELECT row_number() over(ORDER BY a.SNO DESC) rn,a.ChannelNO,a.ONO,a.ChannelPriceReal,a.ChannelNumber+a.ChannelOrder as ChannelName,a.ChannelPrice,a.SNO,b.SPName FROM T_Channel a,T_SPbusi b WITH(NOLOCK) WHERE a.SNO=b.SNO ";
            if (!"-1".Equals(ddlSP.SelectedValue))
                strSQL += string.Format(" AND a.SNO='{0}'", ddlSP.SelectedValue);
            if (!"-1".Equals(ddlChannel.SelectedValue))
                strSQL += string.Format(" AND a.ChannelNO='{0}'", ddlChannel.SelectedValue);
            if (!"-1".Equals(ddlOP.SelectedValue))
                strSQL += string.Format(" AND a.ONO='{0}'", ddlOP.SelectedValue);
            strSQL += " ORDER BY a.SNO";

            //if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
            //    strSQL += string.Format(" AND a.CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

            AspNetPager1.RecordCount = DbHelperSql.GetCount("SELECT COUNT(1) FROM (" + strSQL + ") AS B");
            strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL, (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);
            List<SPPayment> LSPPayment = GetSPPayments(strSQL);


            GVinfo.DataSource = LSPPayment;
            GVinfo.DataBind();
        }
        private List<SPPayment> GetSPPayments(string strSQL)
        {
            List<SPPayment> result = null;
            using (SqlConnection conn = new SqlConnection(BLSqlDataChargeV2.ConnStrV2))
            {
                using (SqlDataReader dr = DbHelperSql.ExecuteReader(conn, strSQL))
                {
                    if (dr != null && dr.HasRows)
                    {
                        result = DTOHelper.CreateModelList<SPPayment>(dr);
                    }
                    else
                    {
                        result = new List<SPPayment>();
                    }
                    dr.Close();
                }
            }
            return result;
        }
        private void SetSPPayments(ref List<SPPayment> LSPPayment)
        {
            if (LSPPayment.Count > 0)
            {
                foreach (SPPayment mSPPayment in LSPPayment)
                {

                }
            }
        }
    }
}
